package de.uni_mannheim;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Logger;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.commons.io.FileUtils;
import org.xml.sax.SAXException;

public class Downloader {
	public static final String BASE_DIR = "/home/bernhard/DataMining";
	public static final String SERVICE_BASE_URL = "http://www.bundesliga.de/data/feed/51/";
	public static final String SCHEDULE = "schedule/schedule.xml";

	private static Logger logger = Logger.getLogger(Downloader.class.getName());

	public static void main(String[] args) throws IOException, SecurityException, SAXException,
			ParserConfigurationException {
		downloadAllStatsForPeriod(2008, 2012);
	}

	private static void downloadAllStatsForPeriod(int fromYear, int toYear) throws SecurityException, IOException,
			SAXException, ParserConfigurationException {
		FileHandler handler = null;
		for (int year = fromYear; year <= toYear; year++) {
			handler = setNewFileHandler(handler, year);
			downloadMatchIds(year);
			File file = new File(BASE_DIR + File.separatorChar + "matchIds" + year + ".txt");
			downloadAllStatsForYear(file, year);
		}
	}

	private static void downloadAllStatsForYear(File file, int year) throws IOException {
		List<String> lines = FileUtils.readLines(file);
		List<Integer> matchIds = new ArrayList<Integer>(lines.size());
		for (String line : lines) {
			matchIds.add(Integer.parseInt(line));
		}

		for (int matchId : matchIds) {

			// Match Facts: match_facts/match_facts_138221.xml
			// Team Stats: team_stats/team_stats_138221.xml
			// Player Stats: player_stats/player_stats_138221.xml
			// Lineup Interval: lineup_interval/lineup_interval_138221.xml
			String dir = BASE_DIR + File.separatorChar + "matches" + year + File.separatorChar + matchId
					+ File.separatorChar;

			for (Stats stat : Stats.values()) {
				downloadMatchStats(stat.name(), matchId, year, dir);
			}
		}
	}

	private static void downloadMatchStats(String name, int matchId, int year, String dir) {
		try {
			URL url = new URL(SERVICE_BASE_URL + year + "/" + name + "/" + name + "_" + matchId + ".xml");
			File file = new File(dir + name + "_" + matchId + ".xml");
			FileUtils.copyURLToFile(url, file);
		} catch (IOException e) {
			logger.warning(e.getMessage());
		}
	}

	/**
	 * The dtd files that might be necessary for sax parsing the xml files can be found at the following url:
	 * http://web.impire.de/SportsML/v1.5/doc/index.html
	 */
	private static void downloadMatchIds(int year) throws IOException, SAXException, ParserConfigurationException {
		URL schedule = new URL(SERVICE_BASE_URL + year + "/schedule/schedule.xml");
		File file = new File(BASE_DIR + File.separatorChar + "schedule" + year + ".xml");
		FileUtils.copyURLToFile(schedule, file);

		SAXParserFactory factory = SAXParserFactory.newInstance();
		factory.setValidating(false);
		factory.setNamespaceAware(false);
		factory.setXIncludeAware(false);
		SAXParser saxParser = factory.newSAXParser();
		ScheduleSaxHandler handler = new ScheduleSaxHandler();
		saxParser.parse(file, handler);
		List<Integer> matchIds = handler.getMatchIds();

		File matchIdsFile = new File(BASE_DIR + File.separatorChar + "matchIds" + year + ".txt");
		FileUtils.writeLines(matchIdsFile, matchIds);
	}

	private static FileHandler setNewFileHandler(FileHandler oldHandler, int year) throws SecurityException,
			IOException {
		if (oldHandler != null) {
			logger.removeHandler(oldHandler);
		}
		FileHandler newHandler = new FileHandler(BASE_DIR + "log" + year + ".txt", true);
		logger.addHandler(newHandler);
		return newHandler;
	}

}
